package com.qin.entity.components;

import com.qin.common.HttpProcessor;
import com.qin.common.RegexProcessor;
import com.qin.db.DBAccess;
import com.qin.entity.ExecuteBean;
import com.qin.entity.ExecuteLog;
import com.qin.entity.SubComponent;
import com.qin.entity.TInterface;
import com.qin.mapper.InterfaceMapper;
import com.qin.service.ExecuteService;
import com.qin.service.impl.ExecuteServiceImpl;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;

public class InterfaceComponent {

    //组件ID
    private String component_id;
    //操作的系统
    private String system_name;
    //接口ID
    private String interface_id;
    //接口入参
    private String input_json;
    //子组件列表
    private List<SubComponent> subComponentList;

    private ExecuteService executeService = new ExecuteServiceImpl();

    public InterfaceComponent(String component_id, String system_name, String interface_id, String input_json, List<SubComponent> subComponentList) {
        this.component_id = component_id;
        this.system_name = system_name;
        this.interface_id = interface_id;
        this.input_json = input_json;
        this.subComponentList = subComponentList;
    }

    public void execute(ExecuteBean executeBean){

        //补全executeBean对象的各属性
        executeBean.setSystem_name(system_name);
        executeBean.complete();
        //设置ExecuteLog对象的各属性
        ExecuteLog executeLog = new ExecuteLog(executeBean.getExecute_id(), executeBean.getFunction_id(),
                executeBean.getTestcase_id(), executeBean.getComponent_id(), "", "");

        //--------------------获取请求url、请求参数--------------------
        //获取系统url
        String system_url = executeBean.getSystem_url();
        //获取接口url
        String interface_url = null;
        try {
            SqlSession sqlSession = DBAccess.getSqlSession();
            InterfaceMapper interfaceMapper = sqlSession.getMapper(InterfaceMapper.class);

            TInterface tInterface = interfaceMapper.queryInterfaces(new TInterface(interface_id)).get(0);

            interface_url = tInterface.getInterface_url();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //拼接请求url
        RegexProcessor regexProcessor = new RegexProcessor();
        String request_url = system_url + interface_url;
        String request_input = regexProcessor.replaceStringByMap(input_json, executeBean.getParaMap());

        //插入日志
        executeService.insertExecuteLog(executeLog, "【请求URL】" + request_url);
        executeService.insertExecuteLog(executeLog, "【请求参数】" + request_input);

        //--------------------请求接口--------------------
        HttpProcessor httpProcessor = new HttpProcessor();
        String request_output = httpProcessor.sendPostRequest(request_url, request_input);

        //插入日志
        executeService.insertExecuteLog(executeLog, "【请求结果】" + request_output);

        //把queryResultList添加到executeBean的dbQueryResultMap中
        executeBean.getInterfaceResultMap().put(component_id, request_output);

        //--------------------遍历执行子组件--------------------
        if(subComponentList != null && subComponentList.size() > 0){
            for(int i=0; i<subComponentList.size(); i++){
                subComponentList.get(i).execute(executeBean);
            }
        }

    }

}
